﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 等边梯形设计器
    /// </summary>
    /// <remarks>
    /// 等边梯形的一条平行边的中心在原点上，另一条平行边的中心在原点+方向的点上
    /// </remarks>
    public class AntiparallelOgramGeoDesigner : GeoDesignerBase
    {
        public AntiparallelOgramGeoDesigner()
        {

            handler4 = new HandlerThumb() { Shape = HandlerThumbShape.Rhombus };
            handler5 = new HandlerThumb() { Shape = HandlerThumbShape.Rhombus };
            this.Handlers.Add(handler4);
            this.Handlers.Add(handler5);
            this.Direction = new Vector(200, 0);
            this.Width1 = 20;
            this.Width2 = 50;

            handler4.DragDelta += Handler4_DragDelta;
            handler5.DragDelta += Handler5_DragDelta;
        }


        #region 控制点拖动处理  
        private void Handler4_DragDelta(object sender, DragDeltaEventArgs e)
        {
            e.Handled = true;
            var v = (this.Direction / 2).Rotate(Math.PI / 2);//旋转90°得到到宽度控制点的向量
            v.Normalize();
            var v1 = (v * this.Width1);
            var change = new Vector(e.HorizontalChange, e.VerticalChange);//拖动的向量
            if (v1.Length == 0)
            {
                this.Width1 = change.Length;
            }
            else
            {
                var prj = v1.Projection(change);//拖动的向量在原始向量上的映射
                this.Width1 = (v1 + prj).Length;
            }

        }
        private void Handler5_DragDelta(object sender, DragDeltaEventArgs e)
        {
            e.Handled = true;
            var v = (this.Direction / 2).Rotate(Math.PI / 2);//旋转90°得到到宽度控制点的向量
            v.Normalize();
            var v2 = (v * this.Width2);
            var change = new Vector(e.HorizontalChange, e.VerticalChange);//拖动的向量
            if (v2.Length == 0)
            {
                this.Width2 = change.Length;
            }
            else
            {
                var prj = v2.Projection(change);//拖动的向量在原始向量上的映射
                this.Width2 = (v2 + prj).Length;
            }

        }

        #endregion


        private readonly HandlerThumb
            handler4,//Width1的控制点
            handler5//Width2的控制点
            ;

        private Point p1, p2, p3, p4;//四个顶点 
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == Width1Property || e.Property == Width2Property)
            {
                rePositionHandlers();
            }
        }
        protected override void rePositionHandlers()
        {
            var op = this.OriginalPoint;
            var target = op + this.Direction;
            var v = (this.Direction / 2).Rotate(Math.PI / 2);//旋转90°得到到宽度控制点的向量
            v.Normalize();

            handler4.Center = this.OriginalPoint + (v * this.Width1);
            handler5.Center = target + (v * this.Width2);

            this.p1 = op + (v * this.Width1);
            this.p2 = target + (v * this.Width2);
            this.p3 = target + (-v * this.Width2);
            this.p4 = op + (-v * this.Width1);

            this.pathGeometry.Clear();
            PathFigure pf = new PathFigure();
            this.pathGeometry.Figures.Add(pf);
            pf.StartPoint = p1;
            pf.Segments.Add(new LineSegment(p2, true));
            pf.Segments.Add(new LineSegment(p3, true));
            pf.Segments.Add(new LineSegment(p4, true));
            pf.IsClosed = true;
            base.rePositionHandlers();
        }

        #region 几何接口

        #endregion

        #region 个性
        /// <summary>
        /// 靠近原点的宽度
        /// </summary>
        public double Width1
        {
            get { return (double)GetValue(Width1Property); }
            set { SetValue(Width1Property, value); }
        }

        public static readonly DependencyProperty Width1Property =
            DependencyProperty.Register("Width1", typeof(double), typeof(AntiparallelOgramGeoDesigner), new PropertyMetadata(default(double)));
        /// <summary>
        /// 原理原点的宽度
        /// </summary>
        public double Width2
        {
            get { return (double)GetValue(Width2Property); }
            set { SetValue(Width2Property, value); }
        }

        public static readonly DependencyProperty Width2Property =
            DependencyProperty.Register("Width2", typeof(double), typeof(AntiparallelOgramGeoDesigner), new PropertyMetadata(default(double)));


        #endregion

    }
}
